class SqlOrder < ActiveRecord::Base
  has_one :sql_content
  belongs_to :user
  belongs_to :database
  validates :user_id, presence: true
  validates :title, length: { minimum: 4, maximum: 60 }
  validates :database_id, presence: true
  validates :status, inclusion: { in: %w(submitted ready fileupload rejected audited vetoed inqueue executing failed incomplete succeeded cancelled) }

  def code_audit?
    self.status && self.status.index('submitted')
  end

  def execute_plan?
    self.status && (self.status.index('failed') or self.status.index('incomplete') or self.status.index('succeeded') or self.status.index('inqueue') or self.status.index('executing'))
  end

  def can_audited?
    self.status && self.status.index('ready')
  end

  def can_cancel?
    self.status && (self.status.index('inqueue') or self.status.index('audited') or self.status.index('ready'))
  end

  #是否可以立即执行
  def can_execute_now?
    self.status && self.status.index('audited')
  end
end
